# coding: utf-8
import warnings
# warnings.filterwarnings('ignore')
import requests
import json

def getStrIndex(str,startStr,endStr):
    startIndex=str.index(startStr)
    endIndex=str.index(endStr,startIndex)
    return [startIndex,startIndex+len(startStr),endIndex]


if __name__ == '__main__':
    postdata = {'key': 'value'}
    requstTemplate='''requireTotalCount=false&currentPage=1&pageSize=12&hasMore=true&totalCount=654&type=2&access_client=PC&orderByClause=last_update_date+desc&category_id=0&category_name=%E5%85%A8%E9%83%A8%E8%AF%BE%E7%A8%8B'''
    cPageIndexInfo=getStrIndex(requstTemplate,'currentPage=','&')#requstTemplate.index('currentPage=')
    pageSizeInfo=getStrIndex(requstTemplate,'pageSize=','&')
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
        'Accept':'application/json, text/javascript',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Content-Length': '174',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': 'uid=CvsUV2NHmp+74yENA0kMAg==; sid=85ee5fb0-a004-4830-ac65-99d9d69332ed; isSupNewWin=true; isOpenNotice=true; utPlayerToken=7a774b49d8a02bce320a32c31a32f553:MjM4OTIxOmh0dHA6Ly9tb29jLmJhb3N0ZWVsLmNvbTo4MC9xbS9Q',
    }
    classList=[]
    for i in range(1,55):
        requstInfo=requstTemplate[0:cPageIndexInfo[1]]+str(i)+requstTemplate[cPageIndexInfo[2]:-1]
        print(requstInfo)
        res = requests.post('http://mooc.baosteel.com/qm/api/v5/course/colist', data=requstInfo,headers=headers)
        print(str(res.content,'UTF-8'))
        results=json.loads(str(res.content,'UTF-8'))['data']['list']
        for result in results:
            classList.append(result)
    classList.sort(key=lambda x:float(x['lesson_periods']))
    print('------------------------------------------------------------')
    print(classList)
    # res = requests.post('http://mooc.baosteel.com/qm/api/v5/course/colist', data=requstTemplate.replace('${}'))
    # print(res.content)

